﻿/*
 * Created by SharpDevelop.
 * User: Andrew
 * Date: 05/05/2013
 * Time: 01:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;

using RRRSRoguelike.Entities.Creatures;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Helpers;
using RRRSRoguelike.Helpers.Visualizer;
using RRRSRoguelike.Managers;
using RRRSRoguelikeData;

namespace RRRSRoguelike.Screens
{
	/// <summary>
	/// Description of ProfileScreen.
	/// </summary>
	public class CustomMonsterScreen:Screen
	{
		Panel MainPanel;
		TileData customMonster = new TileData();
		string fileName;
		DungeonData data;

        #region Constructors
		
        /// <summary>
        /// Gets a profile name from user.
        /// </summary>
        public CustomMonsterScreen()
		{
			PanelList.Add(new Panel(new Point(4, 3),
				Visualizer.GetScreenWidth() - 9, Visualizer.GetScreenHeight() - 6));
			MainPanel = PanelList[0];
			fileName = "Vanilla";
			data = Files.ReadDataFromXML(Path.Combine(Properties.Settings.Default.CustomFolder, fileName,fileName+".xml"));
		}

        #endregion

        #region Methods

        /// <summary>
        /// Show screen
        /// </summary>
        public override void Show()
		{
			Clear();
			this.MainPanel.Clear();
			MainPanel.Draw();
			
				
				customMonster.Name = GetInputFromUser(new Point(35,5),"Enter name:",10);
				
				while(data.Creatures.ContainsKey(customMonster.Name))
				{
					
					customMonster.Name = GetInputFromUser(new Point(35,5),"Monster Exists! Enter another name:",10);
				}
					
				customMonster.Description = GetInputFromUser(new Point(35,5),"Enter description:",256);
				
				customMonster.ImageCharacter = GetInputFromUser(new Point(35,5),"Enter display character:",1);
				
				customMonster.ForeColor = GetInputFromUser(new Point(35,5),"Enter Forecolor:",15);
				
				while(!Constants.ColorsDictionary.ContainsKey(customMonster.ForeColor))
				{
					MainPanel.Clear();
					MainPanel.Write("Valid Colors:");
				int nextline = 1;
				foreach (KeyValuePair<string,ConsoleColor> kvp in Constants.ColorsDictionary)
				{
					
						MainPanel.Write(kvp.Key,nextline);
						nextline++;
				}
					
						WaitKey();
						MainPanel.Clear();
					customMonster.ForeColor = GetInputFromUser(new Point(35,5),"Enter Forecolor:",15);
				}
				
				customMonster.BackColor = GetInputFromUser(new Point(35,5),"Enter Backcolor:",15);

				while(!Constants.ColorsDictionary.ContainsKey(customMonster.BackColor))
				{
					MainPanel.Clear();
					MainPanel.Write("Valid Colors:");
				int nextline = 1;
				foreach (KeyValuePair<string,ConsoleColor> kvp in Constants.ColorsDictionary)
				{
					
						MainPanel.Write(kvp.Key,nextline);
						nextline++;
				}
					
						WaitKey();
						MainPanel.Clear();
					customMonster.BackColor = GetInputFromUser(new Point(35,5),"Enter Backcolor:",15);
				}


				
			
			this.MainPanel.Clear();
			//HACK: should be a culture string
			this.MainPanel.Write("Name: " + customMonster.Name,1);
			this.MainPanel.Write("Description: " + customMonster.Description,2);
			this.MainPanel.Write("Image:",3);
			Console.BackgroundColor = Constants.ColorsDictionary[customMonster.BackColor];
			this.MainPanel.Write(customMonster.ImageCharacter,4,Constants.ColorsDictionary[customMonster.ForeColor]);
			Console.BackgroundColor=ConsoleColor.Black;
			
			data.Creatures.Add(customMonster.Name,customMonster);
			Files.WriteDataToXML(Path.Combine(Properties.Settings.Default.CustomFolder, fileName, fileName + ".xml"),data);
			WaitKey();

			ApplicationManager.Instance.SoundManager.PlaySound(SoundList.Pluck.ToString());


		}

		private string GetInputFromUser(Point cursorPosition,string prompt, int maxLength)
		{
			MainPanel.Clear();

			
			MainPanel.Write(prompt);
			
			//FIXME:should simply move cursor to after the question
			Console.SetCursorPosition(cursorPosition.X,cursorPosition.Y);
			string input = Console.ReadLine();
			
			if (input.Length > maxLength) 
			{
				GameManager.Instance.SoundManager.PlaySound(SoundList.Pluck.ToString());
				MainPanel.Clear();
				//HACK: should be a culture string
				MainPanel.Write("Should be less than " + maxLength.ToString() + " characters.");
				WaitKey();
				input = GetInputFromUser(cursorPosition,prompt,maxLength);
			}

			
			return input;

				
		}
		#endregion
	}
}
